package com.pzh.guli.service.user.controller.api;


import cn.pzh.guli.service.utils.JwtInfo;
import cn.pzh.guli.service.utils.JwtUtil;
import com.pzh.guli.service.base.dto.MemberDto;
import com.pzh.guli.service.base.handler.HandlerException;
import com.pzh.guli.service.user.pojo.Member;
import com.pzh.guli.service.user.pojo.vo.LoginVo;
import com.pzh.guli.service.user.pojo.vo.MemberVo;
import com.pzh.guli.service.user.service.IMemberService;
import com.pzh.guli.service.base.result.Result;
import com.pzh.guli.service.user.service.imp.MemberServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xzh
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/api/member")
public class MemberController {
    @Resource
    private IMemberService iMemberService;
    //注册模块
    @PostMapping("register")
    public Result register(@RequestBody MemberVo memberVo) throws HandlerException {
      iMemberService.register(memberVo);
      return Result.ok().message("用户注册成功");
  }
  //登录模块
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo) throws HandlerException {
        String token = iMemberService.login(loginVo);
        return Result.ok().message("登录成功,欢迎你回来!").data("token",token);
    }
    //获取登录信息
    @GetMapping("get-login-info")
    public Result getUserInfo(HttpServletRequest request){
        JwtInfo jwtInfo = JwtUtil.getMemberInfoByToken(request);
        return Result.ok().data("info",jwtInfo);
    }
    //订单拉取会员信息
    @GetMapping("inner/get-member-dto/{memberId}")
    public MemberDto getMemberDto(@PathVariable String memberId){
        Member member = iMemberService.getById(memberId);
        MemberDto memberDto=new MemberDto();
        BeanUtils.copyProperties(member,memberDto);
        return memberDto;
    }
}

